package fichier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class Client {
	private int ID;
	private String nom;
	private String adresse1;
	private String adresse2;
	private String CP;
	private String ville;
	private static final String table = "client";
    public static final ArrayList < Client > clientList = new ArrayList < Client >();

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public Client(String nom){
		super();
		SelectData.REQUETE = "select * " +
			"from client " +
			"where nom_client = '" + nom + "'";
		ResultSet infoClient = SelectData.startSelect();
		
		try {
			if(infoClient.next());{
				this.setID(Integer.parseInt(infoClient.getString("id_client")));
				this.setNom(infoClient.getString("nom_client"));
				this.setAdresse1(infoClient.getString("adresse1_client"));
				this.setAdresse2(infoClient.getString("adresse2_client"));
				this.setCP(infoClient.getString("cp_client"));
				this.setVille(infoClient.getString("ville_client"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Client(int iD){
		super();
		SelectData.REQUETE = "select * " +
			"from client " +
			"where id_client = " + iD;
		ResultSet infoClient = SelectData.startSelect();
		
		try {
			if(infoClient.next());{
				this.setNom(infoClient.getString("nom_client"));
				this.setAdresse1(infoClient.getString("adresse1_client"));
				this.setAdresse2(infoClient.getString("adresse2_client"));
				this.setCP(infoClient.getString("cp_client"));
				this.setVille(infoClient.getString("ville_client"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Client(int ID, String nom, String adresse1, 
			String adresse2, String CP, String ville) {
		super();
		this.ID = ID;
		this.setNom(SQLadaptString.adapt(nom));
		this.setAdresse1(SQLadaptString.adapt(adresse1));
		this.setAdresse2(SQLadaptString.adapt(adresse2));
		this.setCP(SQLadaptString.adapt(CP));
		this.setVille(SQLadaptString.adapt(ville));
		Client.clientList.add(this);
	}

	public Client(String nom, String adresse1, 
			String adresse2, String CP, String ville) {
		super();
		this.setNom(SQLadaptString.adapt(nom));
		this.setAdresse1(SQLadaptString.adapt(adresse1));
		this.setAdresse2(SQLadaptString.adapt(adresse2));
		this.setCP(SQLadaptString.adapt(CP));
		this.setVille(SQLadaptString.adapt(ville));
		Client.clientList.add(this);
		this.ajouter();
	}
	
	private void ajouter(){
		SelectData.REQUETE  = "INSERT INTO "+ table +" VALUES (null," +
								"'"+ this.getNom() +"', " +
								"'"+this.getAdresse1()+"', " +
								"'"+this.getAdresse2()+"', " +
								"'"+this.getVille()+"', " +
								"'"+this.getCP()+"')";
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
		SelectData.REQUETE = "SELECT MAX(id_client) as 'test' FROM "+ table;
		ResultSet result = SelectData.startSelect();
		String s;
		try {
			while(result.next()){
				s = result.getString("test");
				int lastID = Integer.parseInt( s );
				this.setID(lastID);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void update(){
		if(this.getID()!=0){
			SelectData.REQUETE  = "UPDATE "+ table +" SET " +
									"nom_client='"+ this.getNom() +"'," +
									"adresse1_client= '"+this.getAdresse1()+"'," +
									"adresse2_client= '"+this.getAdresse2()+"'," +
									"cp_client= '"+this.getCP()+"'," +
									"ville_client= '"+this.getVille()+"' " +
									"WHERE " +
									"id_client="+this.getID()+"";
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
		}
	}
	
	public void supprimer(){
		SelectData.REQUETE = "DELETE FROM "+ table +" WHERE id_client ="+this.getID();
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	
	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public String getAdresse1() {
		return adresse1;
	}

	public void setAdresse1(String adresse1) {
		this.adresse1 = adresse1;
	}

	public String getAdresse2() {
		return adresse2;
	}

	public void setAdresse2(String adresse2) {
		this.adresse2 = adresse2;
	}

	public String getCP() {
		return CP;
	}

	public void setCP(String cP) {
		CP = cP;
	}

	public String getVille() {
		return ville;
	}

	public void setVille(String ville) {
		this.ville = ville;
	}
}
